package swimv2gm.client.requests;

import java.io.IOException;
import java.util.Map;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import swimv2gm.bl.session.UserManager;

/**
 * Prepara le competenze di un utente per potergli inviare una richiesta di
 * lavoro.
 * 
 * @author Gallesio Matteo
 */
public class RequestCreation extends HttpServlet {
	private static final long serialVersionUID = 6203757890131484340L;

	protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException {
		UserManager man;
		try {
			man = (UserManager)InitialContext.doLookup("UserManager");
		} catch (NamingException e) {
			response.sendError(500);
			return;
		}
		long dest = -1;
		try {
			dest = Long.parseLong(request.getParameter("dest"));
		} catch (IllegalArgumentException e) {
			request.getRequestDispatcher("sendRequest.jsp").forward(request,response);
			return;
		}
		Long myId = (Long)request.getAttribute("user");

		// block requests to oneself
		if (myId != null && myId == dest) {
			response.sendError(403); // migliore...
			return;
		}

		Map<Long,String> abilities = man.getAbilities(dest);
		request.setAttribute("abilities",abilities);
		request.setAttribute("dest",dest);
		request.getRequestDispatcher("sendRequest.jsp").forward(request,response);
	}

	protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException {
		doGet(request,response);
	}

}
